<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="layui-v2.7.6/layui/css/layui.css">
    <script src="./layui-v2.7.6/layui/layui.js"></script>
    <script src="jq/jquery-3.6.4/jquery-3.6.4.min.js"></script>
</head>

<body>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" required lay-verify="username" placeholder="请输入标题" autocomplete="off"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="text" name="password" required lay-verify="password" placeholder="请输入密码" autocomplete="off"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input type="text" name="repassword" required lay-verify="repassword" placeholder="请输入确认密码"
                    autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <input type="button" class="layui-btn mybtn" value="我是按钮" >

    <script>

        var layer;
        //如果一个页面中需要使用多个模块功能，可以以数组的形式
        layui.use(['form', 'layer'], function () {
            var form = layui.form;
            layer = layui.layer;
           

            // 自定义验证规则 (标签上的lay-verify的值和自定义中的key要一致，才能实现关联)
            form.verify({
                username: function (value) {//value从文本框中获取的值
                    var reg = /^[0-9a-z]\w{3,5}[0-9a-z]$/;//用户名必须是5-7位，并且以数字或者字母开头或者结尾
                    if (!reg.test(value)) {
                        return "用户名必须是5-7位，并且以数字或者字母开头或者结尾";
                    }
                },
                password: [
                    /^[a-z]{6,10}$/
                    , '密码必须6到10位'
                ],
                repassword: function (value) {//确认密码的值
                    var password = $("input[name='password']").val();
                    if (password != value) {
                        return "两次密码输入不一致";
                    }
                }

            });
            //监听提交
            form.on('submit(formDemo)', function (data) {
                // layer.msg(JSON.stringify(data.field));
                return false;
            });
        });

        // function doOpen() {
        //     layer.open({
        //         type:1,
        //         content:"aaa"
        //     });
        // }
        $(".mybtn").click(function(){
            layer.open({
                type:1,
                content:"aaa"
            });
        });


     
    </script>
</body>

</html>